﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

namespace Fish_Atlantis.ProfessorPanel
{
    public partial class ProfessorView1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化第1个DropDownList里的内容

            String searchPeriodStart = "Select DISTINCT Period From Transaction_Record Order By Period ASC;";
            DataSet ds1 = new DataSet();
            ds1 = Helper.GetDataSet(searchPeriodStart, "TablePeriodStart");
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                DropDownList1.Items.Add(new ListItem(ds1.Tables[0].Rows[i]["Period"].ToString(), i.ToString()));
            }
                
            //DropDownList1.DataBind();

            //初始化第2个DropDownList里的内容

            String searchPeriodEnd = "Select DISTINCT Period From Transaction_Record Order By Period ASC;";
            DataSet ds2 = new DataSet();
            ds2 = Helper.GetDataSet(searchPeriodEnd, "TablePeriodEnd");
            for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
            {
                DropDownList2.Items.Add(new ListItem(ds2.Tables[0].Rows[i]["Period"].ToString(), i.ToString()));
            }

            DropDownList2.DataBind();

            //初始化第3个DropDownList里的内容

            String searchCategory = "Select DISTINCT Category From Fishery Order By Category ASC;";
            DataSet ds3 = new DataSet();
            ds3 = Helper.GetDataSet(searchCategory, "TableCategory");
            DropDownList3.Items.Add("Any");
            for (int i = 0; i < ds3.Tables[0].Rows.Count; i++)
            {
                DropDownList3.Items.Add(new ListItem(ds3.Tables[0].Rows[i]["Category"].ToString(), i.ToString()));
            }

            DropDownList3.DataBind();

            //初始化第4个DropDownList里的内容

            String searchFishery = "Select DISTINCT FisheryName From Fishery Order By FisheryName ASC;";
            DataSet ds4 = new DataSet();
            ds4 = Helper.GetDataSet(searchFishery, "TableFishery");
            DropDownList4.Items.Add("Any");
            for (int i = 0; i < ds4.Tables[0].Rows.Count; i++)
            {
                DropDownList4.Items.Add(new ListItem(ds4.Tables[0].Rows[i]["FisheryName"].ToString(), i.ToString()));
            }

            DropDownList4.DataBind();

            //初始化第5个DropDownList里的内容

            String searchCustomer = "Select DISTINCT UserName From Users,Transaction_Record " +
                                    "Where Users.UserID=Transaction_Record.CustomerID " +
                                    "Order By UserName ASC;";
            DataSet ds5 = new DataSet();
            ds5 = Helper.GetDataSet(searchCustomer, "TableCustomer");
            DropDownList5.Items.Add("Any");
            for (int i = 0; i < ds5.Tables[0].Rows.Count; i++)
            {
                DropDownList5.Items.Add(new ListItem(ds5.Tables[0].Rows[i]["UserName"].ToString(), i.ToString()));
            }

            DropDownList5.DataBind();

            //初始化GridView中的内容
            String show = "Select Category As 鱼类,FisheryName As Fishery,Period As 交易时间,"+
                          "Amount As 数量,UserName As 消费者 "+
                          "From Fishery,Transaction_Record,Users " +
                          "Where Fishery.FisheryID=Transaction_Record.FisheryID " +
                          "AND Transaction_Record.CustomerID=Users.UserID "+
                          "Order By Period DESC;";
            DataSet ds6 = new DataSet();
            ds6 = Helper.GetDataSet(show, "TableShow");
            GridView1.DataSource = ds6;
            GridView1.DataBind();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //当第1个DropDownList中的内容选定时，第2个DropDownList中的内容应该随之变化，第二个时间应该晚于第一个
            /*String select1 = DropDownList1.SelectedItem.Text;

            Response.Write(Helper.MessageBox(select1, HttpContext.Current.Request.Url.PathAndQuery));
            String searchPeriodEnd = "Select DISTINCT Period From Transaction_Record " +
                                     "Where Period>'" + select1 + "' " +
                                     "Order By Period ASC;";
            DataSet ds2 = new DataSet();
            ds2 = Helper.GetDataSet(searchPeriodEnd, "TablePeriodEnd");
            for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
            {
                DropDownList2.Items.Add(new ListItem(ds2.Tables[0].Rows[i]["Period"].ToString(), i.ToString()));
            }

            DropDownList2.DataBind();*/
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            //当第3个DropDownList中的内容选定时，第4个DropDownList中的内容应随之变化，可选Fishery会受到限制
            /*String select2 = DropDownList3.SelectedItem.Text;
            
            String searchFishery = "Select DISTINCT FisheryName From Fishery " +
                                   "Where Category='" + select2 + "' Order By FisheryName ASC;";
            DataSet ds4 = new DataSet();
            ds4 = Helper.GetDataSet(searchFishery, "TableFishery");
            DropDownList4.Items.Add("Any");
            for (int i = 0; i < ds4.Tables[0].Rows.Count; i++)
            {
                DropDownList4.Items.Add(new ListItem(ds4.Tables[0].Rows[i]["FisheryName"].ToString(), i.ToString()));
            }

            DropDownList4.DataBind();*/
        }

        protected void CheckButton_Click(object sender, EventArgs e)
        {
            //当单击“查找”按钮时，实现GridView内容的更新
            String SelectPeriodStart = DropDownList1.SelectedItem.Text;
            String SelectPeriodEnd = DropDownList2.SelectedItem.Text;
            String SelectCategory = DropDownList3.SelectedItem.Text;
            String SelectFishery = DropDownList4.SelectedItem.Text;
            String SelectCustomer = DropDownList5.SelectedItem.Text;

            String searchshow = "Select Category As 鱼类,FisheryName As Fishery,Period As 交易时间,"+
                                "Amount As 数量,UserName As 消费者 " +
                                "From Fishery,Transaction_Record,Users " +
                                "Where Fishery.FisheryID=Transaction_Record.FisheryID " +
                                "AND Transaction_Record.CustomerID=Users.UserID " +
                                "AND Period>='" + SelectPeriodStart + "' " +
                                "AND Period<='" + SelectPeriodEnd + "' ";
            if (DropDownList3.SelectedIndex != 0)
            {
                searchshow += "AND Category='" + SelectCategory + "' ";
            }
            if (DropDownList4.SelectedIndex != 0)
            {
                searchshow += "AND FisheryName='" + SelectFishery + "' ";
            }
            if (DropDownList5.SelectedIndex != 0)
            {
                searchshow += "AND UserName='" + SelectCustomer + "' ";
            }
            searchshow += "Order By Period DESC;";

            DataSet dsshow = new DataSet();
            dsshow = Helper.GetDataSet(searchshow, "TableSearch");
            GridView1.DataSource = dsshow;
            GridView1.DataBind();

            //将DropDownList中的内容清空，方便重新加载
            DropDownList1.Items.Clear();
            DropDownList2.Items.Clear();
            DropDownList3.Items.Clear();
            DropDownList4.Items.Clear();
            DropDownList5.Items.Clear();

            //将可选内容重新加载
            String searchPeriodStart = "Select DISTINCT Period From Transaction_Record Order By Period ASC;";
            DataSet ds1 = new DataSet();
            ds1 = Helper.GetDataSet(searchPeriodStart, "TablePeriodStart");
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                DropDownList1.Items.Add(new ListItem(ds1.Tables[0].Rows[i]["Period"].ToString(), i.ToString()));
            }

            String searchPeriodEnd = "Select DISTINCT Period From Transaction_Record Order By Period ASC;";
            DataSet ds2 = new DataSet();
            ds2 = Helper.GetDataSet(searchPeriodEnd, "TablePeriodEnd");
            for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
            {
                DropDownList2.Items.Add(new ListItem(ds2.Tables[0].Rows[i]["Period"].ToString(), i.ToString()));
            }

            String searchCategory = "Select DISTINCT Category From Fishery Order By Category ASC;";
            DataSet ds3 = new DataSet();
            ds3 = Helper.GetDataSet(searchCategory, "TableCategory");
            DropDownList3.Items.Add("Any");
            for (int i = 0; i < ds3.Tables[0].Rows.Count; i++)
            {
                DropDownList3.Items.Add(new ListItem(ds3.Tables[0].Rows[i]["Category"].ToString(), i.ToString()));
            }

            DropDownList3.DataBind();

            String searchFishery = "Select DISTINCT FisheryName From Fishery Order By FisheryName ASC;";
            DataSet ds4 = new DataSet();
            ds4 = Helper.GetDataSet(searchFishery, "TableFishery");
            DropDownList4.Items.Add("Any");
            for (int i = 0; i < ds4.Tables[0].Rows.Count; i++)
            {
                DropDownList4.Items.Add(new ListItem(ds4.Tables[0].Rows[i]["FisheryName"].ToString(), i.ToString()));
            }

            String searchCustomer = "Select DISTINCT UserName From Users,Transaction_Record " +
                                   "Where Users.UserID=Transaction_Record.CustomerID " +
                                   "Order By UserName ASC;";
            DataSet ds5 = new DataSet();
            ds5 = Helper.GetDataSet(searchCustomer, "TableCustomer");
            DropDownList5.Items.Add("Any");
            for (int i = 0; i < ds5.Tables[0].Rows.Count; i++)
            {
                DropDownList5.Items.Add(new ListItem(ds5.Tables[0].Rows[i]["UserName"].ToString(), i.ToString()));
            }

            DropDownList5.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }
    }
}